package com.gx.mxbbx.controller;

import com.github.pagehelper.PageInfo;
import com.gx.mxbbx.po.MxbPolicy;
import com.gx.mxbbx.po.MxbSort;
import com.gx.mxbbx.service.MxbPolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class MxbPolicyController {
    @Autowired
    private MxbPolicyService mxbPolicyService;

    @RequestMapping("/getpolicyList")
    public String getpolicyList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String policyYear = request.getParameter("context");
        currentPage = currentPage != null ? currentPage : 1;
        limit = limit != null ? limit : 10;

        PageInfo<MxbPolicy> pageInfo = mxbPolicyService.getpolicyList(policyYear, currentPage, limit);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("context", policyYear);
        return "/mxbpolicy/mxbpolicy_list";
    }

    @RequestMapping("/openmxbpolicylAdd")
    public String openmxbpolicylAdd(Model model) {
        return "/mxbpolicy/mxbpolicy_add";
    }

    @RequestMapping("/mxbpolicyadd")
    public String mxbpolicyadd(MxbPolicy mxbPolicy, HttpServletRequest request, Model model) {
        mxbPolicyService.mxbpolicyadd(mxbPolicy);
        return getpolicyList(1, 10, request, model);
    }

    @RequestMapping("/mxbpolicydelete")
    public String mxbpolicydelete(Integer id, HttpServletRequest request, Model model) {
        mxbPolicyService.mxbpolicydelete(id);
        return getpolicyList(1, 10, request, model);
    }

    @RequestMapping("/openmxbpolicyEdit")
    public String openmxbpolicyEdit(Integer id, Model model) {
        MxbPolicy mxbPolicy = mxbPolicyService.openmxbpolicyEdit(id);
        model.addAttribute("mxbPolicy", mxbPolicy);
        return "/mxbpolicy/mxbpolicy_edit";
    }

    @RequestMapping("/mxbpolicyedit")
    public String mxbpolicyedit(MxbPolicy mxbPolicy, HttpServletRequest request, Model model) {
        mxbPolicyService.mxbpolicyedit(mxbPolicy);
        return getpolicyList(1, 10, request, model);
    }


}
